package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.SecurityUtils;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.framework.web.service.TokenService;
import com.ticket.system.domain.TUserShare;
import com.ticket.system.service.ITUserShareService;
import com.ticket.system.service.ITYanchuPointsRuleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 用户分享Controller
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/system/share")
public class TUserShareController extends BaseController
{
    @Autowired
    private ITUserShareService tUserShareService;
    @Autowired
    private ITYanchuPointsRuleService itYanchuPointsRuleService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询用户分享列表
     */
    @PreAuthorize("@ss.hasPermi('system:share:list')")
    @GetMapping("/list")
    public TableDataInfo list(TUserShare tUserShare)
    {
        startPage();
        List<TUserShare> list = tUserShareService.selectTUserShareList(tUserShare);
        return getDataTable(list);
    }

    /**
     * 导出用户分享列表
     */
    @PreAuthorize("@ss.hasPermi('system:share:export')")
    @Log(title = "用户分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserShare tUserShare)
    {
        List<TUserShare> list = tUserShareService.selectTUserShareList(tUserShare);
        ExcelUtil<TUserShare> util = new ExcelUtil<TUserShare>(TUserShare.class);
        util.exportExcel(response, list, "用户分享数据");
    }

    /**
     * 获取用户分享详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:share:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tUserShareService.selectTUserShareById(id));
    }

    /**
     * 新增用户分享
     */
    @PreAuthorize("@ss.hasPermi('system:share:add')")
    @Log(title = "用户分享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUserShare tUserShare)
    {
        return toAjax(tUserShareService.insertTUserShare(tUserShare));
    }

    /**
     * 修改用户分享
     */
    @PreAuthorize("@ss.hasPermi('system:share:edit')")
    @Log(title = "用户分享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUserShare tUserShare)
    {
        return toAjax(tUserShareService.updateTUserShare(tUserShare));
    }

    /**
     * 删除用户分享
     */
    @PreAuthorize("@ss.hasPermi('system:share:remove')")
    @Log(title = "用户分享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tUserShareService.deleteTUserShareByIds(ids));
    }


    @PostMapping("/addPointsShare")
    @ApiOperation("分享增加积分")
    public AjaxResult addPointsShare(@RequestBody TUserShare tUserShare, HttpServletRequest request)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tUserShare.setUserId(loginUser.getUser().getUserId());
        List<TUserShare> tUserShares = tUserShareService.selectTUserShareList(tUserShare);
        //首次分享添加积分
        if(CollectionUtils.isEmpty(tUserShares) && null != loginUser){
            itYanchuPointsRuleService.addPoints(7,null,loginUser.getUser().getUserId());
        }
        tUserShare.setCreateTime(new Date());
        tUserShare.setUpdateTime(new Date());
        tUserShareService.insertTUserShare(tUserShare);
        return AjaxResult.success();
    }
}
